#!/usr/bin/env python
#coding=utf-8
# author:wade
# contact: 317909531@qq.com
# datetime:2020/4/25 0:52
from rest_framework import serializers
from shareAPI import models
"""
文件说明：
# 群增与群改反序列化实现
# 1）ModelSerializer默认不通过群改功能，需要在Meta中设置 list_serializer_class
# 2）自定义ListSerializer子类，重写update方法，将子类绑定给 list_serializer_class
# 3）重写update方法中通过 代表要更新的对象们instance 及 提供的更新数据们validated_data
#       得到 更新后的对象们instance 返回
"""
# 群增与群改反序列化实现
# 1）ModelSerializer默认不通过群改功能，需要在Meta中设置 list_serializer_class
# 2）自定义ListSerializer子类，重写update方法，将子类绑定给 list_serializer_class
# 3）重写update方法中通过 代表要更新的对象们instance 及 提供的更新数据们validated_data
#       得到 更新后的对象们instance 返回


# 获取所有当前所有的账号(嵌套不支持直接反序列化)
class B_Score_serializers(serializers.ModelSerializer):
    id = serializers.IntegerField(source='fenxiangid')
    class Meta:
        model = models.Score
        fields = '__all__'
        # fields = ['CLASS_NUM', 'ID']
        depth = 3

# 首页查询列表推送
class B_Questions_serializers(serializers.ModelSerializer):
    class Meta:
        model = models.Questions
        # fields = '__all__'
        fields = ['id', 'title']
        depth = 3

#单个问题内容查询
class B_Questions_serializers_Item(serializers.ModelSerializer):
    def to_representation(self, instance):  #对于空值进行处理，防止给前端的是null
        data = super().to_representation(instance)
        print(data)
        option = dict()
        option["A"] = data["A"]
        option["B"] = data["B"]
        option["C"] = data["C"]
        for i in data:
            if not data[i] and data[i] != 0:
                print(data[i])
                data[i] = ""
        data['option'] = option
        data.pop('A')
        data.pop('B')
        data.pop('C')
        # print(option)

        return data

    class Meta:
        model = models.QItem
        # fields = '__all__'
        fields = ['id', 'question','A','B','C','qItem_questions_id']

        depth = 1

class B_Score_serializers_NoDepth_listSerialzer(serializers.ListSerializer):
    def update(self, instance, validated_data):
        '''
        :param instance: [book_obj1, ..., book_obj2]
        :param validated_data: [{更新数据的字段}, ..., {更新数据的字段}]
        :return: [book_new_obj1, ..., book_new_obj2]
        '''
        for index, obj in enumerate(instance):   # type :int, models.Score
            # 单个对象数据更新 - 一个个属性更新值
            for attr, value in validated_data[index].items():
                # 对象有更新数据字典的key对应的属性，才完成该属性的值更新
                if hasattr(obj, attr):
                    setattr(obj, attr, value)
            # 信息同步到数据库
            obj.save()
        return instance


# 获取所有当前所有的账号
class B_Score_serializers_NoDepth(serializers.ModelSerializer):
    id = serializers.IntegerField(source='fenxiangid')

    class Meta:
        model = models.Score
        fields = '__all__'
        # fields = ['CLASS_NUM', 'ID']
        list_serializer_class = B_Score_serializers_NoDepth_listSerialzer
    # def validate(self, attrs): 对 对象进行验证
    # def validate_xxx字段名(self,value): #对字段进行验证

# 获取所有分享的类型
class B_Type_serializers(serializers.ModelSerializer):
    id = serializers.IntegerField(source='typeid')
    class Meta:
        model = models.Type
        fields = '__all__'


class B_QingHuiType_serializers(serializers.ModelSerializer):
    class Meta:
        model = models.QingHuiType
        fields = '__all__'
        depth = 2

class B_RandomUserInfo_serializers(serializers.ModelSerializer):
    class Meta:
        model = models.RandomUserInfo
        fields = '__all__'
        depth = 2